package com.ewili.x.special.task.thread.impl;

import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.x.special.dao.inter.ExecsDao;
import com.ewili.x.special.entity.Execs;
import com.ewili.x.special.task.thread.inter.ExecsThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author wonder
 */
@Slf4j
@Component
public class ExecsThreadTaskImpl implements ExecsThreadTask {

    @Autowired
    private ExecsDao execsDao;


    @Override
    //@Async("myTaskAsyncPool")
    public void spideExecs(String url) {
        String[][] objs;
        try {
            objs = ObjectHttpClientUtils.formatEastMoneyFormatData(url);
        } catch (Exception e) {
            log.error("url:{}", url, e);
            return;
        }
        if (objs == null) {
            return;
        }

        //Date maxDate = execsDao.findMaxDate();

        for (int i = 0; i < objs.length; i++) {
            try {
                Date date = DateUtils.parseDate(objs[i][5], DateUtils.yyyy_MM_dd);
                String code = objs[i][2];
                String changePeople = objs[i][1];
                Double changeStocks = Double.valueOf(objs[i][6]);
                Double avgPrice = Double.valueOf(objs[i][8]);
                Double changeAmount = Double.valueOf(objs[i][13]);
                String changeReason = objs[i][12];
                if (changeReason == null || changeReason.isEmpty()) {
                    changeReason = "竞价交易";
                }
                String obj = objs[i][0];
                if (obj.contains("\"")) {
                    obj = obj.replace("\"", "");
                }
                Double changeRate = Double.valueOf(obj);
                Double afterChangeStocks = Double.valueOf(objs[i][7]);
                String execsName = objs[i][1];
                String duties = objs[i][14];
                String relationship = objs[i][10];

//                if (maxDate != null && DateUtils.isSameDay(maxDate, date)) {
//                    break;
//                }

                boolean isExecsExists = execsDao.existsByCodeAndDateAndChangePeopleAndChangeStocksAndChangeReason(code, date, changePeople, changeStocks, changeReason);

                if (isExecsExists) {
                    continue;
                }

                Execs execs = new Execs();
                execs.setCode(code);
                execs.setDate(date);
                execs.setChangePeople(changePeople);
                execs.setChangeStocks(changeStocks);
                execs.setAvgPrice(avgPrice);
                execs.setChangeAmount(changeAmount);
                execs.setChangeReason(changeReason);
                execs.setChangeRate(changeRate);
                execs.setAfterChangeStocks(afterChangeStocks);
                execs.setExecsName(execsName);
                execs.setDuties(duties);
                execs.setRelationship(relationship);

                execsDao.saveAndFlush(execs);

            } catch (Exception e) {
                log.error("url:{}", url, e);
            }
        }

    }
}
